package org.pluslibs.framework.swingplus.filechooser;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import org.apache.commons.lang.StringUtils;

public class JFileChooserFactory {
    private static final String EXTENSION_SEPERATOR_FORMAT = ".%s";
    public static FileFilter XML = new FileFilter() {
        @Override
        public String getDescription() {
            return "XML";
        }

        @Override
        public boolean accept(File f) {
            return StringUtils.endsWith(f.getName(), ".xml");
        }
    };

    public static JFileChooser buildFileAndDirectoryChooser(String approveText, FileFilter ff) {
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        fc.setMultiSelectionEnabled(true);
        fc.setApproveButtonText(approveText);
        fc.setAcceptAllFileFilterUsed(false);
        fc.setFileFilter(ff);
        return fc;
    }

    public static FileFilter buildFileFilter(final String fileTypeDescription, final String fileExtensionWithoutDot) {
        return new FileFilter() {
            @Override
            public String getDescription() {
                return fileTypeDescription;
            }

            @Override
            public boolean accept(File f) {
                return StringUtils.endsWith(f.getName(), String.format(EXTENSION_SEPERATOR_FORMAT, fileExtensionWithoutDot));
            }
        };
    }
}